
-- Agregar comm.tipocom en el SELECT (habia habido un olvido)

CREATE OR REPLACE FUNCTION tg_artm_ins_artmpre()
  RETURNS trigger AS
$BODY$
DECLARE
vfecha       varchar;
vtipoprepaga comm.tipoprepaga%TYPE;
vcodprepag   comm.codprepag%TYPE;
vtipopada    pad.tipopada%TYPE;
vcodpadr     pad.codpadr%TYPE;
vidpad       comm.idpad%TYPE;
vtipoarta    art.tipoarta%TYPE;
vcodartr     art.codartr%TYPE;
vcodambsuc   comm.codambsuc%TYPE;
vcodambtur   comm.codambtur%TYPE;
vnrocierre   comm.nrocierre%TYPE; 
vtipocom     comm.tipocom%TYPE;
/* En el alta de artm, guarda valores de precios historicos para cada varpre que
 * tenga pre.artmpre = 'S'
 */
BEGIN
SELECT CAST(comm.fecha AS VARCHAR), comm.tipoprepaga, comm.codprepag, 
	pad.tipopada, pad.codpadr, comm.idpad,
	NULL, NULL, 
	comm.codambsuc, comm.codambtur, comm.nrocierre, comm.tipocom

INTO vfecha, vtipoprepaga, vcodprepag,
	vtipopada, vcodpadr, vidpad,
	vtipoarta, vcodartr,  
	vcodambsuc, vcodambtur, vnrocierre, vtipocom 
FROM comm INNER JOIN pad ON pad.id = comm.idpad            
WHERE NEW.idcomm = comm.id;

SELECT art.tipoarta, art.codartr 
INTO vtipoarta, vcodartr
FROM art
WHERE art.id = NEW.idart;
/*pre_obtenerprecioctxt(character varying, integer, character varying, character varying, character varying, character varying, character varying, integer, character varying,
character varying, character varying, character varying, character varying, double precision, character varying, integer)  */

INSERT INTO artmpre 
	(SELECT NEW.id, pre.id, pre_obtenerprecioctxt(pre.cod, NEW.idart,
		vfecha, 
		vtipoprepaga, vcodprepag,
		vtipopada, vcodpadr, vidpad,
		vtipoarta, vcodartr,
		vfecha, vfecha,
		vcodambsuc, 0.00,
		vcodambtur, vnrocierre, vtipocom)                                                                   
FROM pre
WHERE pre.artmpre = 'S');

RETURN NEW;
END;
$BODY$
  LANGUAGE plpgsql VOLATILE COST 100;
ALTER FUNCTION tg_artm_ins_artmpre() OWNER TO abacouser;
GRANT EXECUTE ON FUNCTION tg_artm_ins_artmpre() TO abacouser;
GRANT EXECUTE ON FUNCTION tg_artm_ins_artmpre() TO public;



-- Agregar tipocom en llamado a pre_obtenerprecioarticuloinfo (habia habido un olvido) 

-- Function: public.pre_obtenerprecio(character varying, integer, character varying, character varying, character varying, integer)

-- DROP FUNCTION public.pre_obtenerprecio(character varying, integer, character varying, character varying, character varying, integer);

CREATE OR REPLACE FUNCTION public.pre_obtenerprecio(
    character varying,
    integer,
    character varying,
    character varying,
    character varying,
    integer)
  RETURNS numeric AS
$BODY$
DECLARE
vble 		ALIAS FOR $1;
id_ART 		ALIAS FOR $2;
fechaEntrada	ALIAS FOR $3;
codigoSuc	ALIAS FOR $4;
codambtur       ALIAS FOR $5;
nrocierre       ALIAS FOR $6;
Ctxt_TIPOPREPAGA VARCHAR;
Ctxt_CODPREPAG   VARCHAR;
Ctxt_TIPOPADA    VARCHAR;
Ctxt_CODPADR     VARCHAR;
Ctxt_IDPAD       INTEGER;
Ctxt_TIPOARTA    VARCHAR;
Ctxt_CODARTR     VARCHAR;
Ctxt_FECHAINI    VARCHAR;
Ctxt_FECHAFIN    VARCHAR;
Ctxt_CODSUC      VARCHAR;
Ctxt_TIPOCOM     VARCHAR;
valorNUMERAL_CANT VARCHAR;
fechaConsulta	VARCHAR;
arbolArticulo	VARCHAR;
rubroArticulo	VARCHAR;
-- valores de retorno 
precio          NUMERIC(16,4);
dummyinfo       VARCHAR;
BEGIN
IF (COALESCE(fechaEntrada, '') = '') OR (fechaEntrada = '') THEN
	fechaConsulta := CAST(CURRENT_DATE AS VARCHAR);
ELSE
	fechaConsulta := fechaEntrada;
END IF;

SELECT tipoArtA, codArtR
FROM art WHERE (id = id_ART)
INTO arbolArticulo, rubroArticulo;

--contexto vacio
Ctxt_TIPOPREPAGA := '';
Ctxt_CODPREPAG   := '';
Ctxt_TIPOPADA    := '';
Ctxt_CODPADR     := '';
Ctxt_IDPAD       := -1;
Ctxt_TIPOARTA    := arbolArticulo;
Ctxt_CODARTR     := rubroArticulo;
Ctxt_FECHAINI    := fechaConsulta;
Ctxt_FECHAFIN    := fechaConsulta;
Ctxt_CODSUC      := COALESCE(codigoSuc, '');
Ctxt_TIPOCOM     := '';

--valor para la vble de precio  especial  fija #CANT
valorNUMERAL_CANT := '0.0000';

SELECT * FROM pre_ObtenerPrecioArticuloInfo(vble, id_ART, fechaConsulta, Ctxt_TIPOPREPAGA, Ctxt_CODPREPAG,
					    Ctxt_TIPOPADA, Ctxt_CODPADR, Ctxt_IDPAD,
					    Ctxt_TIPOARTA, Ctxt_CODARTR, 
					    Ctxt_FECHAINI, Ctxt_FECHAFIN, Ctxt_CODSUC, valorNUMERAL_CANT,
					    CodAmbTur, NroCierre, Ctxt_TIPOCOM)
INTO precio, dummyinfo;
RETURN precio;
END;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
ALTER FUNCTION public.pre_obtenerprecio(character varying, integer, character varying, character varying, character varying, integer)
  OWNER TO abacouser;
COMMENT ON FUNCTION public.pre_obtenerprecio(character varying, integer, character varying, character varying, character varying, integer) IS 'Funcion llamada desde la app. (ART__Data, RepArt_RM)';


--==================================
SELECT aaVersion('445.03','445.02');
--==================================
